Micron Document
`:top
In some `F33f`_`[programming languages`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Programming_language]`_`f, `!const`! is a `F33f`_`[type qualifier`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Type_qualifier]`_`f (a `F33f`_`[keyword`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Keyword_(computer_programming)]`_`f applied to a `F33f`_`[data type`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Data_type]`_`f) that indicates that the data is read-only. While this can be used to declare `F33f`_`[constants`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Constant_(computer_programming)]`_`f, const in the `F33f`_`[C family`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=List_of_C-family_programming_languages]`_`f of languages differs from similar constructs in other languages in that it is part of the `*type`*, and thus has complicated behavior when combined with `F33f`_`[pointers`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Pointer_(computer_programming)]`_`f, references, `F33f`_`[composite data types`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Composite_data_type]`_`f, and `F33f`_`[type-checking`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Type-checking]`_`f. In other languages, the data is not in a single `F33f`_`[memory location`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Memory_location]`_`f, but copied at `F33f`_`[compile time`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Compile_time]`_`f for each use.`:cite-ref-1[`F5bf`_`[1`#cite-note-1]`_`f] Languages which use it include `F33f`_`[C`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=C_(programming_language)]`_`f, `F33f`_`[C++`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=C++]`_`f, `F33f`_`[D`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=D_(programming_language)]`_`f, `F33f`_`[JavaScript`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=JavaScript]`_`f, `F33f`_`[Julia`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Julia_(programming_language)]`_`f, and `F33f`_`[Rust`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Rust_(programming_language)]`_`f.

>>Contents

• `F0af`_`[Introduction`#introduction]`_`f
• `F0af`_`[Consequences`#consequences]`_`f
• `F0af`_`[Distinction from constants`#distinction-from-constants]`_`f
• `F0af`_`[Other uses`#other-uses]`_`f
• `F0af`_`[Syntax`#syntax]`_`f
• `F0af`_`[Simple data types`#simple-data-types]`_`f
• `F0af`_`[Pointers and references`#pointers-and-references]`_`f
• `F0af`_`[Parameters and variables`#parameters-and-variables]`_`f
• `F0af`_`[C++`#c]`_`f
• `F0af`_`[Methods`#methods]`_`f
• `F0af`_`[Loopholes to const-correctness`#loopholes-to-const-correctness]`_`f
• `F0af`_`[Problems`#problems]`_`f
• `F0af`_`[strchr problem`#strchr-problem]`_`f
• `F0af`_`[D`#d]`_`f
• `F0af`_`[History`#history]`_`f
• `F0af`_`[Other languages`#other-languages]`_`f
• `F0af`_`[See also`#see-also]`_`f
• `F0af`_`[Notes`#notes]`_`f
• `F0af`_`[References`#references]`_`f
• `F0af`_`[External links`#external-links]`_`f

-─

>>Introduction

When applied in an `F33f`_`[object`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Object_(computer_science)]`_`f `F33f`_`[declaration`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Declaration_(computer_programming)]`_`f,`:cite-ref-2[`F5bf`_`[a`#cite-note-2]`_`f] it indicates that the object is a `F33f`_`[constant`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Constant_(computer_programming)]`_`f: its `F33f`_`[value`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Value_(computer_science)]`_`f may not be changed, unlike a `F33f`_`[variable`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Variable_(computer_science)]`_`f. This basic use – to declare constants – has parallels in many other languages.

However, unlike in other languages, in the C family of languages the `B100`F9d9const`f`b is part of the `*type`*, not part of the `*object`*. For example, in C, `B100`F9d9int const x = 1;`f`b declares an object `B100`F9d9x`f`b of `B100`F9d9int const`f`b type – the `B100`F9d9const`f`b is part of the type, as if it were parsed "(int const) x" – while in `F33f`_`[Ada`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Ada_(programming_language)]`_`f, `B100`F9d9X : constant INTEGER := 1_`f`b declares a constant (a kind of object) `B100`F9d9X`f`b of `B100`F9d9INTEGER`f`b type: the `B100`F9d9constant`f`b is part of the `*object`*, but not part of the `*type`*.

This has two subtle results. Firstly, `B100`F9d9const`f`b can be applied to parts of a more complex type – for example, `B100`F9d9int const * const x;`f`b declares a constant pointer to a constant integer, while `B100`F9d9int const * x;`f`b declares a variable pointer to a constant integer, and `B100`F9d9int * const x;`f`b declares a constant pointer to a variable integer. Secondly, because `B100`F9d9const`f`b is part of the type, it must match as part of type-checking. For example, the following code is invalid:

`B100`F9d9void f(int& x);`f`b
`B100`F9d9// ...`f`b
`B100`F9d9int const i;`f`b
`B100`F9d9f(i);`f`b

because the argument to `B100`F9d9f`f`b must be a `*variable`* integer, but `B100`F9d9i`f`b is a `*constant`* integer. This matching is a form of `F33f`_`[program correctness`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Program_correctness]`_`f, and is known as `!const-correctness`!. This allows a form of `F33f`_`[programming by contract`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Programming_by_contract]`_`f, where functions specify as part of their `F33f`_`[type signature`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Type_signature]`_`f whether they modify their arguments or not, and whether their `F33f`_`[return value`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Return_value]`_`f is modifiable or not. This type-checking is primarily of interest in pointers and references – not basic value types like integers – but also for `F33f`_`[composite data types`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Composite_data_type]`_`f or templated types such as `F33f`_`[containers`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Container_(abstract_data_type)]`_`f. It is concealed by the fact that the `B100`F9d9const`f`b can often be omitted, due to `F33f`_`[type coercion`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Type_coercion]`_`f (implicit `F33f`_`[type conversion`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Type_conversion]`_`f) and C being `F33f`_`[call-by-value`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Call-by-value]`_`f (C++ and D are either call-by-value or call-by-reference).

>>Consequences

The idea of const-ness does not imply that the variable as it is stored in `F33f`_`[computer memory`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Computer_memory]`_`f is unwritable. Rather, `B100`F9d9const`f`b-ness is a `F33f`_`[compile-time`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Compile-time]`_`f construct that indicates what a programmer `*should`* do, not necessarily what they `*can`* do. Note, however, that in the case of predefined data (such as `B100`F9d9char const *`f`b `F33f`_`[string literals`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=String_literal]`_`f), C `B100`F9d9const`f`b is `*often`* unwritable.

>>Distinction from constants

While a constant does not change its value while the program is running, an object declared `B100`F9d9const`f`b may indeed change its value while the program is running. A common example are read only registers within embedded systems like the current state of a digital input. The data registers for digital inputs are often declared as `B100`F9d9const`f`b and `B100`F9d9`F33f`_`[volatile`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Volatile_(computer_programming)]`_`f`f`b. The content of these registers may change without the program doing anything (`B100`F9d9volatile`f`b) but it would be ill-formed for the program to attempt write to them (`B100`F9d9const`f`b).

>>Other uses

In addition, a (non-static) member-function can be declared as `B100`F9d9const`f`b. In this case, the `F33f`_`[this pointer`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=This_(computer_programming)]`_`f inside such a function is of type `B100`F9d9object_type const *`f`b rather than merely of type `B100`F9d9object_type *`f`b.`:cite-ref-3[`F5bf`_`[2`#cite-note-3]`_`f] This means that non-const functions for this object cannot be called from inside such a function, nor can `F33f`_`[member variables`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Field_(computer_science)]`_`f be modified. In C++, a member variable can be declared as `B100`F9d9`F33f`_`[mutable`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Mutable_object]`_`f`f`b, indicating that this restriction does not apply to it. In some cases, this can be useful, for example with `F33f`_`[caching`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Cache_(computing)]`_`f, `F33f`_`[reference counting`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Reference_counting]`_`f, and `F33f`_`[data synchronization`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Data_synchronization]`_`f. In these cases, the logical meaning (state) of the object is unchanged, but the object is not physically constant since its bitwise representation may change.

>>Syntax

In C, C++, and D, all data types, including those defined by the user, can be declared `B100`F9d9const`f`b, and const-correctness dictates that all variables or objects should be declared as such unless they need to be modified. Such proactive use of `B100`F9d9const`f`b makes values "easier to understand, track, and reason about",`:cite-ref-4[`F5bf`_`[3`#cite-note-4]`_`f] and it thus increases the readability and comprehensibility of code and makes working in teams and maintaining code simpler because it communicates information about a value's intended use. This can help the `F33f`_`[compiler`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Compiler]`_`f as well as the developer when reasoning about code. It can also enable an `F33f`_`[optimizing compiler`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Optimizing_compiler]`_`f to generate more efficient code.`:cite-ref-5[`F5bf`_`[4`#cite-note-5]`_`f]

>>>Simple data types

For simple non-pointer data types, applying the `B100`F9d9const`f`b qualifier is straightforward. It can go on either side of some types for historical reasons (for example, `B100`F9d9const char foo = 'a';`f`b is equivalent to `B100`F9d9char const foo = 'a';`f`b). On some implementations, using `B100`F9d9const`f`b twice (for instance, `B100`F9d9const char const`f`b or `B100`F9d9char const const`f`b) generates a warning but not an error.

>>>Pointers and references

For pointer and reference types, the meaning of `B100`F9d9const`f`b is more complicated – either the pointer itself, or the value being pointed to, or both, can be `B100`F9d9const`f`b. Further, the syntax can be confusing. A pointer can be declared as a `B100`F9d9const`f`b pointer to writable value, or a writable pointer to a `B100`F9d9const`f`b value, or `B100`F9d9const`f`b pointer to `B100`F9d9const`f`b value. A `B100`F9d9const`f`b pointer cannot be reassigned to point to a different object from the one it is initially assigned, but it can be used to modify the value that it points to (called the `*`F33f`_`[pointee`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Pointee]`_`f`*).`:cite-ref-6[`F5bf`_`[5`#cite-note-6]`_`f]`:cite-ref-7[`F5bf`_`[6`#cite-note-7]`_`f]`:cite-ref-8[`F5bf`_`[7`#cite-note-8]`_`f]`:cite-ref-9[`F5bf`_`[8`#cite-note-9]`_`f]`:cite-ref-10[`F5bf`_`[9`#cite-note-10]`_`f] Reference variables in C++ are an alternate syntax for `B100`F9d9const`f`b pointers. A pointer to a `B100`F9d9const`f`b object, on the other hand, can be reassigned to point to another memory location (which should be an object of the same type or of a convertible type), but it cannot be used to modify the memory that it is pointing to. A `B100`F9d9const`f`b pointer to a `B100`F9d9const`f`b object can also be declared and can neither be used to modify the apointee nor be reassigned to point to another object. The following code illustrates these subtleties:

`B100`F9d9void Foo( int * ptr,`f`b
`B100`F9d9 int const * ptrToConst,`f`b
`B100`F9d9 int * const constPtr,`f`b
`B100`F9d9 int const * const constPtrToConst )`f`b
`B100`F9d9{`f`b
`B100`F9d9 *ptr = 0; // OK: modifies the pointed to data`f`b
`B100`F9d9 ptr = NULL; // OK: modifies the pointer`f`b
`B100`F9d9`f`b
`B100`F9d9 *ptrToConst = 0; // Error! Cannot modify the pointed to data`f`b
`B100`F9d9 ptrToConst = NULL; // OK: modifies the pointer`f`b
`B100`F9d9`f`b
`B100`F9d9 *constPtr = 0; // OK: modifies the pointed to data`f`b
`B100`F9d9 constPtr = NULL; // Error! Cannot modify the pointer`f`b
`B100`F9d9`f`b
`B100`F9d9 *constPtrToConst = 0; // Error! Cannot modify the pointed to data`f`b
`B100`F9d9 constPtrToConst = NULL; // Error! Cannot modify the pointer`f`b
`B100`F9d9}`f`b

>>>>C convention

Following usual C convention for declarations, declaration follows use, and the `B100`F9d9*`f`b in a pointer is written on the pointer, indicating `F33f`_`[dereferencing`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Dereferencing]`_`f. For example, in the declaration `B100`F9d9int *ptr`f`b, the dereferenced form `B100`F9d9*ptr`f`b is an `B100`F9d9int`f`b, while the reference form `B100`F9d9ptr`f`b is a pointer to an `B100`F9d9int`f`b. Thus `B100`F9d9const`f`b modifies the `*name`* to its right. The C++ convention is instead to associate the `B100`F9d9*`f`b with the type, as in `B100`F9d9int* ptr`f`b, and read the `B100`F9d9const`f`b as modifying the `*type`* to the left. `B100`F9d9int const * ptrToConst`f`b can thus be read as "`B100`F9d9*ptrToConst`f`b is a `B100`F9d9int const`f`b" (the value is constant), or "`B100`F9d9ptrToConst`f`b is a `B100`F9d9int const *`f`b" (the pointer is a pointer to a constant integer). Thus:

`B100`F9d9int *ptr; // *ptr is an int value`f`b
`B100`F9d9int const *ptrToConst; // *ptrToConst is a constant (int: integer value)`f`b
`B100`F9d9int * const constPtr; // constPtr is a constant (int *: integer pointer)`f`b
`B100`F9d9int const * const constPtrToConst; // constPtrToConst is a constant pointer and points`f`b
`B100`F9d9 // to a constant value`f`b

>>>>C++ convention

Following C++ convention of analyzing the type, not the value, a `F33f`_`[rule of thumb`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Rule_of_thumb]`_`f is to read the declaration from right to left. Thus, everything to the left of the star can be identified as the pointed type and everything to the right of the star are the pointer properties. For instance, in our example above, `B100`F9d9int const *`f`b can be read as a writable pointer that refers to a non-writable integer, and `B100`F9d9int * const`f`b can be read as a non-writable pointer that refers to a writable integer.

A more generic rule that helps you understand complex declarations and definitions works like this:

1. find the identifier whose declaration you want to understand
2. read as far as possible to the right (i.e., until the end of the declaration or to the next closing parenthesis, whichever comes first)
3. back up to where you began, and read backwards to the left (i.e., until the beginning of the declaration or to the open-parenthesis matching the closing parenthesis found in the previous step)
4. when you've reached the beginning of the declaration you're done. If not, continue at step 2, beyond the closing parenthesis that was matched last.

Here is an example:

`t
| Part of expression | double ( ** const ( * fun ( int ))( double ))[ 10 ] | Meaning (reading downwards) |
|---|---|---|
| Identifier | fun | fun is a ... |
| Read to the right | ( int )) | function expecting an int ... |
| Find the matching ( | ( * | returning a pointer to ... |
| Continue right | ( double )) | a function expecting a double ... |
| Find the matching ( | ( ** const | returning a constant pointer to a pointer to ... |
| Continue right | [ 10 ] | blocks of 10 ... |
| Read to the left | double | doubles. |
`t

When reading to the left, it is important that you read the elements from right to left. So an `B100`F9d9int const *`f`b becomes a `*pointer to a const int`* and not a `*const pointer to an int`*.

In some cases C/C++ allows the `B100`F9d9const`f`b keyword to be placed to the left of the type. Here are some examples:

`B100`F9d9const int *ptrToConst; //identical to: int const *ptrToConst,`f`b
`B100`F9d9const int *const constPtrToConst; //identical to: int const *const constPtrToConst`f`b

Although C/C++ allows such definitions (which closely match the English language when reading the definitions from left to right), the compiler still reads the definitions according to the abovementioned procedure: from right to left. But putting `B100`F9d9const`f`b `*before`* what must be constant quickly introduces mismatches between what you intend to write and what the compiler decides you wrote. Consider pointers to pointers:

`B100`F9d9int **ptr; // a pointer to a pointer to ints`f`b
`B100`F9d9int const **ptr // a pointer to a pointer to constant int value`f`b
`B100`F9d9 // (not a pointer to a constant pointer to ints)`f`b
`B100`F9d9int *const *ptr // a pointer to a const pointer to int values`f`b
`B100`F9d9 // (not a constant pointer to a pointer to ints)`f`b
`B100`F9d9int **const ptr // a constant pointer to pointers to ints`f`b
`B100`F9d9 // (ptr, the identifier, being const makes no sense)`f`b
`B100`F9d9int const **const ptr // a constant pointer to pointers to constant int values`f`b

As a final note regarding pointer definitions: always write the pointer symbol (the *) as much as possible to the right. Attaching the pointer symbol to the type is tricky, as it strongly suggests a pointer type, which isn't the case. Here are some examples:

`B100`F9d9int* a; /* write: */ int *a; // a is a pointer to an int`f`b
`B100`F9d9int* a, b; // CONFUSING`f`b
`B100`F9d9 /* write: */ int *a, b; // a is a pointer to an int,`f`b
`B100`F9d9 // but b is a mere int`f`b
`B100`F9d9int* a, *b; // UGLY: both a and b are pointers to ints`f`b
`B100`F9d9 /* write: */ int *a, *b;`f`b

Bjarne Stroustrup's FAQ recommends only declaring one variable per line if using the C++ convention, to avoid this issue.`:cite-ref-11[`F5bf`_`[10`#cite-note-11]`_`f]

The same considerations apply to defining references and rvalue references:

`B100`F9d9int var = 22;`f`b
`B100`F9d9int const &refToConst = var; // OK`f`b
`B100`F9d9int const& ref2 = var, ref3 = var; // CONFUSING:`f`b
`B100`F9d9 // ref2 is a reference, but ref3 isn't:`f`b
`B100`F9d9 // ref3 is a constant int initialized with`f`b
`B100`F9d9 // var's value`f`b
`B100`F9d9int &const constRef = var; // ERROR: as references can't change anyway.`f`b
`B100`F9d9`f`b
`B100`F9d9// C++:`f`b
`B100`F9d9int&& rref = int(5), value = 10; // CONFUSING:`f`b
`B100`F9d9 // rref is an rvalue reference, but value is`f`b
`B100`F9d9 // a mere int.`f`b
`B100`F9d9 /* write: */ int &&rref = int(5), value = 10;`f`b

More complicated declarations are encountered when using multidimensional arrays and references (or pointers) to pointers. Although it is sometimes argued that such declarations are confusing and error-prone and that they therefore should be avoided or be replaced by higher-level structures, the procedure described at the top of this section can always be used without introducing ambiguities or confusion.

>>>Parameters and variables

`B100`F9d9const`f`b can be declared both on function parameters and on variables (`F33f`_`[static`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Static_variable]`_`f or automatic, including global or local). The interpretation varies between uses. A `B100`F9d9const`f`b static variable (global variable or static local variable) is a constant, and may be used for data like mathematical constants, such as `B100`F9d9double const PI = 3.14159`f`b – realistically longer, or overall compile-time parameters. A `B100`F9d9const`f`b automatic variable (non-static local variable) means that `F33f`_`[single assignment`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Single_assignment]`_`f is happening, though a different value may be used each time, such as `B100`F9d9int const x_squared = x * x`f`b. A `B100`F9d9const`f`b parameter in pass-by-reference means that the referenced value is not modified – it is part of the `F33f`_`[contract`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Design_by_contract]`_`f – while a `B100`F9d9const`f`b parameter in pass-by-value (or the pointer itself, in pass-by-reference) does not add anything to the interface (as the value has been copied), but indicates that internally, the function does not modify the local copy of the parameter (it is a single assignment). For this reason, some favor using `B100`F9d9const`f`b in parameters only for pass-by-reference, where it changes the contract, but not for pass-by-value, where it exposes the implementation.

>>C++

>>>Methods

In order to take advantage of the `F33f`_`[design by contract`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Design_by_contract]`_`f approach for user-defined types (structs and classes), which can have methods as well as member data, the programmer may tag instance methods as `B100`F9d9const`f`b if they don't modify the object's data members. Applying the `B100`F9d9const`f`b qualifier to instance methods thus is an essential feature for const-correctness, and is not available in many other `F33f`_`[object-oriented`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Object-oriented_programming]`_`f languages such as `F33f`_`[Java`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Java_(programming_language)]`_`f and `F33f`_`[C#`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=C_Sharp_(programming_language)]`_`f or in `F33f`_`[Microsoft`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Microsoft]`_`f's `F33f`_`[C++/CLI`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=C++/CLI]`_`f or `F33f`_`[Managed Extensions for C++`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Managed_Extensions_for_C++]`_`f. While `B100`F9d9const`f`b methods can be called by `B100`F9d9const`f`b and non-`B100`F9d9const`f`b objects alike, non-`B100`F9d9const`f`b methods can only be invoked by non-`B100`F9d9const`f`b objects. The `B100`F9d9const`f`b modifier on an instance method applies to the object pointed to by the "`B100`F9d9`F33f`_`[this`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=This_(computer_science)]`_`f`f`b" pointer, which is an implicit argument passed to all instance methods. Thus having `B100`F9d9const`f`b methods is a way to apply const-correctness to the implicit "`B100`F9d9this`f`b" pointer argument just like other arguments.

This example illustrates:

`B100`F9d9class C`f`b
`B100`F9d9{`f`b
`B100`F9d9 int i;`f`b
`B100`F9d9public:`f`b
`B100`F9d9 int Get() const // Note the "const" tag`f`b
`B100`F9d9 { return i; }`f`b
`B100`F9d9 void Set(int j) // Note the lack of "const"`f`b
`B100`F9d9 { i = j; }`f`b
`B100`F9d9};`f`b
`B100`F9d9`f`b
`B100`F9d9void Foo(C& nonConstC, C const& constC)`f`b
`B100`F9d9{`f`b
`B100`F9d9 int y = nonConstC.Get(); // Ok`f`b
`B100`F9d9 int x = constC.Get(); // Ok: Get() is const`f`b
`B100`F9d9`f`b
`B100`F9d9 nonConstC.Set(10); // Ok: nonConstC is modifiable`f`b
`B100`F9d9 constC.Set(10); // Error! Set() is a non-const method and constC is a const-qualified object`f`b
`B100`F9d9}`f`b

In the above code, the implicit "`B100`F9d9this`f`b" pointer to `B100`F9d9Set()`f`b has the type "`B100`F9d9C *const`f`b"; whereas the "`B100`F9d9this`f`b" pointer to `B100`F9d9Get()`f`b has type "`B100`F9d9C const *const`f`b", indicating that the method cannot modify its object through the "`B100`F9d9this`f`b" pointer.

Often the programmer will supply both a `B100`F9d9const`f`b and a non-`B100`F9d9const`f`b method with the same name (but possibly quite different uses) in a class to accommodate both types of callers. Consider:

`B100`F9d9class MyArray`f`b
`B100`F9d9{`f`b
`B100`F9d9 int data[100];`f`b
`B100`F9d9public:`f`b
`B100`F9d9 int & Get(int i) { return data[i]; }`f`b
`B100`F9d9 int const & Get(int i) const { return data[i]; }`f`b
`B100`F9d9};`f`b
`B100`F9d9`f`b
`B100`F9d9void Foo( MyArray & array, MyArray const & constArray )`f`b
`B100`F9d9{`f`b
`B100`F9d9 // Get a reference to an array element`f`b
`B100`F9d9 // and modify its referenced value.`f`b
`B100`F9d9`f`b
`B100`F9d9 array.Get( 5 ) = 42; // OK! (Calls: int & MyArray::Get(int))`f`b
`B100`F9d9 constArray.Get( 5 ) = 42; // Error! (Calls: int const & MyArray::Get(int) const)`f`b
`B100`F9d9}`f`b

The `B100`F9d9const`f`b-ness of the calling object determines which version of `B100`F9d9MyArray::Get()`f`b will be invoked and thus whether or not the caller is given a reference with which he can manipulate or only observe the private data in the object. The two methods technically have different signatures because their "`B100`F9d9this`f`b" pointers have different types, allowing the compiler to choose the right one. (Returning a `B100`F9d9const`f`b reference to an `B100`F9d9int`f`b, instead of merely returning the `B100`F9d9int`f`b by value, may be overkill in the second method, but the same technique can be used for arbitrary types, as in the `F33f`_`[Standard Template Library`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Standard_Template_Library]`_`f.)

>>Loopholes to const-correctness

There are several loopholes to pure const-correctness in C and C++. They exist primarily for compatibility with existing code.

The first, which applies only to C++, is the use of `B100`F9d9const_cast`f`b, which allows the programmer to strip the `B100`F9d9const`f`b qualifier, making any object modifiable. The necessity of stripping the qualifier arises when using existing code and libraries that cannot be modified but which are not const-correct. For instance, consider this code:

`B100`F9d9// Prototype for a function which we cannot change but which`f`b
`B100`F9d9// we know does not modify the pointee passed in.`f`b
`B100`F9d9void LibraryFunc(int* ptr, int size);`f`b
`B100`F9d9`f`b
`B100`F9d9void CallLibraryFunc(int const * ptr, int size)`f`b
`B100`F9d9{`f`b
`B100`F9d9 LibraryFunc(ptr, size); // Error! Drops const qualifier`f`b
`B100`F9d9`f`b
`B100`F9d9 int* nonConstPtr = const_cast<int*>(ptr); // Strip qualifier`f`b
`B100`F9d9 LibraryFunc(nonConstPtr, size); // OK`f`b
`B100`F9d9}`f`b

However, any attempt to modify an object that is itself declared `B100`F9d9const`f`b by means of a `F33f`_`[const cast`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Const_cast]`_`f results in undefined behavior according to the ISO C++ Standard. In the example above, if `B100`F9d9ptr`f`b references a global, local, or member variable declared as `B100`F9d9const`f`b, or an object allocated on the heap via `B100`F9d9new int const`f`b, the code is only correct if `B100`F9d9LibraryFunc`f`b really does not modify the value pointed to by `B100`F9d9ptr`f`b.

The C language has a need of a loophole because a certain situation exists. Variables with static storage duration are allowed to be defined with an initial value. However, the initializer can use only constants like string constants and other literals, and is not allowed to use non-constant elements like variable names, whether the initializer elements are declared `B100`F9d9const`f`b or not, or whether the static duration variable is being declared `B100`F9d9const`f`b or not. There is a non-portable way to initialize a `B100`F9d9const`f`b variable that has static storage duration. By carefully constructing a typecast on the left hand side of a later assignment, a `B100`F9d9const`f`b variable can be written to, effectively stripping away the `B100`F9d9const`f`b attribute and 'initializing' it with non-constant elements like other `B100`F9d9const`f`b variables and such. Writing into a `B100`F9d9const`f`b variable this way may work as intended, but it causes undefined behavior and seriously contradicts const-correctness:

`B100`F9d9size_t const bufferSize = 8*1024;`f`b
`B100`F9d9size_t const userTextBufferSize; // initial value depends on const bufferSize, can't be initialized here`f`b
`B100`F9d9`f`b
`B100`F9d9...`f`b
`B100`F9d9`f`b
`B100`F9d9int setupUserTextBox(textBox_t *defaultTextBoxType, rect_t *defaultTextBoxLocation)`f`b
`B100`F9d9{`f`b
`B100`F9d9 *(size_t*)&userTextBufferSize = bufferSize - sizeof(struct textBoxControls); // warning: might work, but not guaranteed by C`f`b
`B100`F9d9 ...`f`b
`B100`F9d9}`f`b

Another loophole`:cite-ref-12[`F5bf`_`[11`#cite-note-12]`_`f] applies both to C and C++. Specifically, the languages dictate that member pointers and references are "shallow" with respect to the `B100`F9d9const`f`b-ness of their owners – that is, a containing object that is `B100`F9d9const`f`b has all `B100`F9d9const`f`b members except that member pointees (and referees) are still mutable. To illustrate, consider this C++ code:

`B100`F9d9struct S`f`b
`B100`F9d9{`f`b
`B100`F9d9 int val;`f`b
`B100`F9d9 int *ptr;`f`b
`B100`F9d9};`f`b
`B100`F9d9`f`b
`B100`F9d9void Foo(S const & s)`f`b
`B100`F9d9{`f`b
`B100`F9d9 int i = 42;`f`b
`B100`F9d9 s.val = i; // Error: s is const, so val is a const int`f`b
`B100`F9d9 s.ptr = &i; // Error: s is const, so ptr is a const pointer to int`f`b
`B100`F9d9 *s.ptr = i; // OK: the data pointed to by ptr is always mutable,`f`b
`B100`F9d9 // even though this is sometimes not desirable`f`b
`B100`F9d9}`f`b

Although the object `B100`F9d9s`f`b passed to `B100`F9d9Foo()`f`b is constant, which makes all of its members constant, the pointee accessible through `B100`F9d9s.ptr`f`b is still modifiable, though this may not be desirable from the standpoint of `B100`F9d9const`f`b-correctness because `B100`F9d9s`f`b might solely own the pointee. For this reason, Meyers argues that the default for member pointers and references should be "deep" `B100`F9d9const`f`b-ness, which could be overridden by a `B100`F9d9mutable`f`b qualifier when the pointee is not owned by the container, but this strategy would create compatibility issues with existing code. Thus, for historical reasons, this loophole remains open in C and C++.

The latter loophole can be closed by using a class to hide the pointer behind a `B100`F9d9const`f`b-correct interface, but such classes either do not support the usual copy semantics from a `B100`F9d9const`f`b object (implying that the containing class cannot be copied by the usual semantics either) or allow other loopholes by permitting the stripping of `B100`F9d9const`f`b-ness through inadvertent or intentional copying.

Finally, several functions in the `F33f`_`[C standard library`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=C_standard_library]`_`f violate const-correctness before `F33f`_`[C23`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=C23_(C_standard_revision)]`_`f, as they accept a `B100`F9d9const`f`b pointer to a character string and return a non-`B100`F9d9const`f`b pointer to a part of the same string. `B100`F9d9`F33f`_`[strstr`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Strstr]`_`f`f`b and `B100`F9d9`F33f`_`[strchr`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Strchr]`_`f`f`b are among these functions. Some implementations of the C++ standard library, such as Microsoft's`:cite-ref-13[`F5bf`_`[12`#cite-note-13]`_`f] try to close this loophole by providing two `F33f`_`[overloaded`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Function_overloading]`_`f versions of some functions: a "`B100`F9d9const`f`b" version and a "non-`B100`F9d9const`f`b" version.

>>Problems

The use of the type system to express constancy leads to various complexities and problems, and has accordingly been criticized and not adopted outside the narrow C family of C, C++, and D. Java and C#, which are heavily influenced by C and C++, both explicitly rejected `B100`F9d9const`f`b-style type qualifiers, instead expressing constancy by keywords that apply to the identifier (`B100`F9d9final`f`b in Java, `B100`F9d9const`f`b and `B100`F9d9readonly`f`b in C#). Even within C and C++, the use of `B100`F9d9const`f`b varies significantly, with some projects and organizations using it consistently, and others avoiding it.

>>>strchr problem

The `B100`F9d9const`f`b type qualifier causes difficulties when the logic of a function is agnostic to whether its input is constant or not, but returns a value which should be of the same qualified type as an input. In other words, for these functions, if the input is constant (const-qualified), the return value should be as well, but if the input is variable (not `B100`F9d9const`f`b-qualified), the return value should be as well. Because the `F33f`_`[type signature`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Type_signature]`_`f of these functions differs, it requires two functions (or potentially more, in case of multiple inputs) with the same logic – a form of `F33f`_`[generic programming`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Generic_programming]`_`f.

This problem arises even for simple functions in the C standard library, notably `B100`F9d9strchr`f`b; this observation is credited by Ritchie to Tom Plum in the mid 1980s.`:cite-ref-14[`F5bf`_`[13`#cite-note-14]`_`f] The `B100`F9d9strchr`f`b function locates a character in a string; formally, it returns a pointer to the first occurrence of the character `B100`F9d9c`f`b in the string `B100`F9d9s`f`b, and in classic C (K&R C) its prototype is:

`B100`F9d9char *strchr(char *s, int c);`f`b

The `B100`F9d9strchr`f`b function does not modify the input string, but the return value is often used by the caller to modify the string, such as:

`B100`F9d9if (p = strchr(q, '/'))`f`b
`B100`F9d9 *p = ' ';`f`b

Thus on the one hand the input string `*can`* be `B100`F9d9const`f`b (since it is not modified by the function), and if the input string is `B100`F9d9const`f`b the return value should be as well – most simply because it might return exactly the input pointer, if the first character is a match – but on the other hand the return value should not be `B100`F9d9const`f`b if the original string was not `B100`F9d9const`f`b, since the caller may wish to use the pointer to modify the original string.

In C++ this is done via `F33f`_`[function overloading`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Function_overloading]`_`f, typically implemented via a `F33f`_`[template`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Template_(C++)]`_`f, resulting in two functions, so that the return value has the same `B100`F9d9const`f`b-qualified type as the input:`:cite-ref-15[`F5bf`_`[b`#cite-note-15]`_`f]

`B100`F9d9char* strchr(char* s, int c);`f`b
`B100`F9d9char const* strchr(char const* s, int c);`f`b

These can in turn be defined by a template:

`B100`F9d9template <T>`f`b
`B100`F9d9T* strchr(T* s, int c) { ... }`f`b

In D this is handled via the `B100`F9d9inout`f`b keyword, which acts as a wildcard for const, immutable, or unqualified (variable), yielding:`:cite-ref-d8-8-16-0[`F5bf`_`[14`#cite-note-d8-8-16]`_`f]`:cite-ref-17[`F5bf`_`[c`#cite-note-17]`_`f]

`B100`F9d9inout(char)* strchr(inout(char)* s, int c);`f`b

However, in C neither of these is possible`:cite-ref-18[`F5bf`_`[d`#cite-note-18]`_`f] since C does not have function overloading, and instead, this is handled by having a single function where the input is constant but the output is writable:

`B100`F9d9char *strchr(char const *s, int c);`f`b

This allows idiomatic C code but does strip the const qualifier if the input actually was const-qualified, violating type safety. This solution was proposed by Ritchie and subsequently adopted. This difference is one of the failures of `F33f`_`[compatibility of C and C++`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Compatibility_of_C_and_C++]`_`f.

Since `F33f`_`[C23`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=C23_(C_standard_revision)]`_`f, this problem is solved with the use of the `B100`F9d9_Generic`f`b facility of the language: the identifiers of `B100`F9d9strchr`f`b and the other functions affected by the issue have been turned into macros that expand a call to an appropriate function which will return a `B100`F9d9const`f`b pointer if one was passed to them and an unqualified pointer if an unqualified pointer was passed to them.`:cite-ref-n3020-19-0[`F5bf`_`[15`#cite-note-n3020-19]`_`f]

>>D

In version 2 of the `F33f`_`[D programming language`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=D_(programming_language)]`_`f, two keywords relating to const exist.`:cite-ref-20[`F5bf`_`[16`#cite-note-20]`_`f] The `B100`F9d9immutable`f`b keyword denotes data that cannot be modified through any reference. The `B100`F9d9const`f`b keyword denotes a non-mutable view of mutable data. Unlike C++ `B100`F9d9const`f`b, D `B100`F9d9const`f`b and `B100`F9d9immutable`f`b are "deep" or `F33f`_`[transitive`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Transitive_relation]`_`f, and anything reachable through a `B100`F9d9const`f`b or `B100`F9d9immutable`f`b object is `B100`F9d9const`f`b or `B100`F9d9immutable`f`b respectively.

`!Example of const vs. immutable in D`!

`B100`F9d9int[] foo = new int[5]; // foo is mutable.`f`b
`B100`F9d9const int[] bar = foo; // bar is a const view of mutable data.`f`b
`B100`F9d9immutable int[] baz = foo; // Error: all views of immutable data must be immutable.`f`b
`B100`F9d9`f`b
`B100`F9d9immutable int[] nums = new immutable(int)[5]; // No mutable reference to nums may be created.`f`b
`B100`F9d9const int[] constNums = nums; // Works. immutable is implicitly convertible to const.`f`b
`B100`F9d9int[] mutableNums = nums; // Error: Cannot create a mutable view of immutable data.`f`b

`!Example of transitive or deep const in D`!

`B100`F9d9class Foo {`f`b
`B100`F9d9 Foo next;`f`b
`B100`F9d9 int num;`f`b
`B100`F9d9}`f`b
`B100`F9d9`f`b
`B100`F9d9immutable Foo foo = new immutable(Foo);`f`b
`B100`F9d9foo.next.num = 5; // Won't compile. foo.next is of type immutable(Foo).`f`b
`B100`F9d9 // foo.next.num is of type immutable(int).`f`b

>>History

`B100`F9d9const`f`b was introduced by `F33f`_`[Bjarne Stroustrup`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Bjarne_Stroustrup]`_`f in `F33f`_`[C with Classes`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=C_with_Classes]`_`f, the predecessor to `F33f`_`[C++`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=C++]`_`f, in 1981, and was originally called `B100`F9d9readonly`f`b.`:cite-ref-21[`F5bf`_`[17`#cite-note-21]`_`f]`:cite-ref-siblings-22-0[`F5bf`_`[18`#cite-note-siblings-22]`_`f] As to motivation, Stroustrup writes:`:cite-ref-siblings-22-1[`F5bf`_`[18`#cite-note-siblings-22]`_`f]

"It served two functions: as a way of defining a symbolic constant that obeys scope and type rules (that is, without using a macro) and as a way of deeming an object in memory immutable."

The first use, as a scoped and typed alternative to macros, was analogously fulfilled for function-like macros via the `B100`F9d9inline`f`b keyword. Constant pointers, and the `B100`F9d9* const`f`b notation, were suggested by Dennis Ritchie and so adopted.`:cite-ref-siblings-22-2[`F5bf`_`[18`#cite-note-siblings-22]`_`f]

`B100`F9d9const`f`b was then adopted in C as part of standardization, and appears in `F33f`_`[C89`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=ANSI_C]`_`f (and subsequent versions) along with the other type qualifier, `B100`F9d9volatile`f`b.`:cite-ref-23[`F5bf`_`[19`#cite-note-23]`_`f] A further qualifier, `B100`F9d9noalias`f`b, was suggested at the December 1987 meeting of the X3J11 committee, but was rejected; its goal was ultimately fulfilled by the `B100`F9d9`F33f`_`[restrict`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Restrict]`_`f`f`b keyword in `F33f`_`[C99`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=C99]`_`f. Ritchie was not very supportive of these additions, arguing that they did not "carry their weight", but ultimately did not argue for their removal from the standard.`:cite-ref-24[`F5bf`_`[20`#cite-note-24]`_`f]

D subsequently inherited `B100`F9d9const`f`b from C++, where it is known as a `*type constructor`* (not `F33f`_`[type qualifier`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Type_qualifier]`_`f) and added two further type constructors, `B100`F9d9immutable`f`b and `B100`F9d9inout`f`b, to handle related use cases.`:cite-ref-25[`F5bf`_`[e`#cite-note-25]`_`f]

>>Other languages

Other languages do not follow C/C++ in having constancy part of the type, though they often have superficially similar constructs and may use the `B100`F9d9const`f`b keyword. Typically this is only used for constants (constant objects).

`F33f`_`[C#`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=C_Sharp_(programming_language)]`_`f has a `B100`F9d9const`f`b keyword, but with radically different and simpler semantics: it means a compile-time constant, and is not part of the type.

`F33f`_`[Nim`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Nim_(programming_language)]`_`f has a `B100`F9d9const`f`b keyword similar to that of C#: it also declares a compile-time constant rather than forming part of the type. However, in Nim, a constant can be declared from any expression that can be evaluated at compile time.`:cite-ref-26[`F5bf`_`[21`#cite-note-26]`_`f] In C#, only C# built-in types can be declared as `B100`F9d9const`f`b; user-defined types, including classes, structs, and arrays, cannot be `B100`F9d9const`f`b.`:cite-ref-27[`F5bf`_`[22`#cite-note-27]`_`f]

`F33f`_`[Java`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Java_(programming_language)]`_`f does not have `B100`F9d9const`f`b – it instead has `B100`F9d9final`f`b, which can be applied to local "variable" declarations and applies to the `*identifier`*, not the type. It has a different object-oriented use for object members, which is the origin of the name.

The Java language specification regards `B100`F9d9const`f`b as a reserved keyword – i.e., one that cannot be used as variable identifier – but assigns no semantics to it: it is a `*reserved word`* (it cannot be used in identifiers) but not a `*keyword`* (it has no special meaning). The keyword was included as a means for Java compilers to detect and warn about the incorrect usage of C++ keywords.`:cite-ref-28[`F5bf`_`[23`#cite-note-28]`_`f] An enhancement request ticket for implementing `B100`F9d9const`f`b correctness exists in the `F33f`_`[Java Community Process`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Java_Community_Process]`_`f, but was closed in 2005 on the basis that it was impossible to implement in a backwards-compatible fashion.`:cite-ref-29[`F5bf`_`[24`#cite-note-29]`_`f]

The contemporary `F33f`_`[Ada 83`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Ada_83]`_`f independently had the notion of a constant object and a `B100`F9d9constant`f`b keyword,`:cite-ref-30[`F5bf`_`[25`#cite-note-30]`_`f]`:cite-ref-31[`F5bf`_`[f`#cite-note-31]`_`f] with `F33f`_`[input parameters`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Input_parameter]`_`f and loop parameters being implicitly constant. Here the `B100`F9d9constant`f`b is a property of the object, not of the type.

`F33f`_`[JavaScript`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=JavaScript]`_`f has a `B100`F9d9const`f`b declaration that defines a `F33f`_`[block-scoped`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Block_scope]`_`f variable that cannot be reassigned nor redeclared. It defines a read-only reference to a variable that cannot be redefined, but in some situations the value of the variable itself may potentially change, such as if the variable refers to an object and a property of it is altered.`:cite-ref-32[`F5bf`_`[26`#cite-note-32]`_`f]

>>See also

• `F33f`_`[Single assignment`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Single_assignment]`_`f
• `F33f`_`[restrict`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Restrict]`_`f
• `F33f`_`[Pointer aliasing`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Pointer_aliasing]`_`f

>>Notes

`:cite-note-2`!a.`! `F0af`_`[↑`#cite-ref-2]`_`f Formally when the `B100`F9d9const`f`b is part of the outermost derived type in a declaration; pointers complicate discussion.
`:cite-note-15`!b.`! `F0af`_`[↑`#cite-ref-15]`_`f Note that pointer declaration syntax conventions differ between C and C++: in C `B100`F9d9char *s`f`b is standard, while in C++ `B100`F9d9char* s`f`b is standard.
`:cite-note-17`!c.`! `F0af`_`[↑`#cite-ref-17]`_`f Idiomatic D code would use an array here instead of a pointer.`:cite-ref-d8-8-16-1[`F5bf`_`[14`#cite-note-d8-8-16]`_`f]
`:cite-note-18`!d.`! `F0af`_`[↑`#cite-ref-18]`_`f In C11 and later `B100`F9d9_Generic`f`b could have been used to implement a const-correct `B100`F9d9strchr`f`b. char* strchr_m(char *s, int c); char const* strchr_c(char const *s, int c); #define strchr(X,Y) _Generic((X), \\ char*: strchr_m, \\ const char*: strchr_c \\ )(X,Y)
`:cite-note-25`!e.`! `F0af`_`[↑`#cite-ref-25]`_`f D also introduced the `B100`F9d9shared`f`b type constructor, but this is related to use cases of `B100`F9d9volatile`f`b, not `B100`F9d9const`f`b.
`:cite-note-31`!f.`! `F0af`_`[↑`#cite-ref-31]`_`f The Ada standard calls this a "`F33f`_`[reserved word`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Reserved_word]`_`f"; see that article for usage.

>>References

`:cite-note-1`!1.`! `F0af`_`[↑`#cite-ref-1]`_`f "Constant items – The Rust Reference". `*doc.rust-lang.org`*. Retrieved 2022-06-22.
`:cite-note-3`!2.`! `F0af`_`[↑`#cite-ref-3]`_`f "The `B100`F9d9this`f`b pointer". `*Draft C++ Standard`*. Retrieved 2020-03-30. The type of `B100`F9d9this`f`b in a member function whose type has a `*cv-qualifier-seq cv`* and whose class is `B100`F9d9X`f`b is "pointer to `*cv`* `B100`F9d9X`f`b".
`:cite-note-4`!3.`! `F0af`_`[↑`#cite-ref-4]`_`f `F33f`_`[Herb Sutter`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Herb_Sutter]`_`f and `F33f`_`[Andrei Alexandrescu`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Andrei_Alexandrescu]`_`f (2005). `*C++ Coding Standards`*. p. 30. Boston: Addison Wesley. `F33f`_`[ISBN`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=ISBN_(identifier)]`_`f 0-321-11358-6
`:cite-note-5`!4.`! `F0af`_`[↑`#cite-ref-5]`_`f "Why is the kfree() argument const?". lkml.org. 2013-01-12.
`:cite-note-6`!5.`! `F0af`_`[↑`#cite-ref-6]`_`f "5.1. Extensions implemented in GNU Fortran: 5.1.16 Cray pointers". `*The GNU Fortran Compiler`*. 2006. Archived from the original on 2022-12-21. Retrieved 2022-12-21.
`:cite-note-7`!6.`! `F0af`_`[↑`#cite-ref-7]`_`f `:citereffaheynagle1999`aFahey, Mark R.; Nagle, Dan (1999-04-19). "Cray Fortran Pointers vs. Fortran 90 Pointers and Porting from the Cray C90 to the SGI Origin2000" (PDF). Vicksburg, Massachusetts, USA: US Army Corps of Engineers Waterways Experiment Station, Major Shared Resource Center. Archived (PDF) from the original on 2022-12-23. Retrieved 2022-12-23. (8 pages)
`:cite-note-8`!7.`! `F0af`_`[↑`#cite-ref-8]`_`f "Appendix C: Fortran 90 Features and Differences > Features > Cray Pointers". `*Fortran User's Guide`*. `F33f`_`[Oracle Corporation`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Oracle_Corporation]`_`f. 2010. Archived from the original on 2021-09-21. Retrieved 2022-12-23.
`:cite-note-9`!8.`! `F0af`_`[↑`#cite-ref-9]`_`f "Appendix C: Fortran 90 Features and Differences > Features > Cray Character Pointers". `*Fortran User's Guide`*. `F33f`_`[Oracle Corporation`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Oracle_Corporation]`_`f. 2010. Archived from the original on 2022-12-23. Retrieved 2022-12-23.
`:cite-note-10`!9.`! `F0af`_`[↑`#cite-ref-10]`_`f "Chapter 4. Data Types". `*Fortran Language Reference Manual, Volume 1`*. Vol. 1. `F33f`_`[Silicon Graphics, Inc.`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Silicon_Graphics,_Inc.]`_`f 1999 [1993]. Document Number: 007-3692-004. Archived from the original on 2022-12-23. Retrieved 2022-12-23. (NB. Derived from "FORTRAN 90 HANDBOOK" (1992, `F33f`_`[McGraw-Hill, Inc.`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=McGraw-Hill,_Inc.]`_`f) by Walter S. Brainerd, Jeanne C. Adams, Jeanne T. Martin, Brian T. Smith, and Jerrold L. Wagener.)
`:cite-note-11`!10.`! `F0af`_`[↑`#cite-ref-11]`_`f "Stroustrup: C++ Style and Technique FAQ".
`:cite-note-12`!11.`! `F0af`_`[↑`#cite-ref-12]`_`f `F33f`_`[Scott Meyers`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Scott_Meyers]`_`f (2005). `*Effective C++, Third Edition`*. pp. 21–23. Boston: Addison Wesley. `F33f`_`[ISBN`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=ISBN_(identifier)]`_`f 978-0-321-33487-9
`:cite-note-13`!12.`! `F0af`_`[↑`#cite-ref-13]`_`f "strchr, wcschr, _mbschr (CRT)". Msdn.microsoft.com. Retrieved 2017-11-23.
`:cite-note-14`!13.`! `F0af`_`[↑`#cite-ref-14]`_`f "Dennis Ritchie: Why I do not like X3J11 type qualifiers".
`:cite-note-d8-8-16`!14.`! `F0af`_`[↑`#cite-ref-d8-8-16-0]`_`f `*The D Programming Language,`* `F33f`_`[Andrei Alexandrescu`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Andrei_Alexandrescu]`_`f, 8.8: Propagating a Qualifier from Parameter to Result
`:cite-note-n3020-19`!15.`! `F0af`_`[↑`#cite-ref-n3020-19-0]`_`f "WG14-N3020 : Qualifier-preserving standard library functions" (PDF). `*open-std.org`*. 2022-06-13. Archived (PDF) from the original on 2022-10-13.
`:cite-note-20`!16.`! `F0af`_`[↑`#cite-ref-20]`_`f "const(FAQ) – D Programming Language". Digitalmars.com. Retrieved 2013-08-18.
`:cite-note-21`!17.`! `F0af`_`[↑`#cite-ref-21]`_`f `F33f`_`[Bjarne Stroustrup`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Bjarne_Stroustrup]`_`f, "Extensions of the C Language Type Concept", Bell Labs internal Technical Memorandum, January 5, 1981.
`:cite-note-siblings-22`!18.`! `F0af`_`[↑`#cite-ref-siblings-22-0]`_`f Sibling Rivalry: C and C++, `F33f`_`[Bjarne Stroustrup`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Bjarne_Stroustrup]`_`f, 2002, p. 5
`:cite-note-23`!19.`! `F0af`_`[↑`#cite-ref-23]`_`f `F33f`_`[Dennis M. Ritchie`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Dennis_M._Ritchie]`_`f, "The Development of the C Language Archived July 15, 2012, at `F33f`_`[archive.today`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Archive.today]`_`f", 2003: "X3J11 also introduced a host of smaller additions and adjustments, for example, the type qualifiers `!const`! and `!volatile`!, and slightly different type promotion rules."
`:cite-note-24`!20.`! `F0af`_`[↑`#cite-ref-24]`_`f "Let me begin by saying that I'm not convinced that even the pre-December qualifiers ('const' and 'volatile') carry their weight; I suspect that what they add to the cost of learning and using the language is not repaid in greater expressiveness. 'Volatile', in particular, is a frill for esoteric applications, and much better expressed by other means. Its chief virtue is that nearly everyone can forget about it. 'Const' is simultaneously more useful and more obtrusive; you can't avoid learning about it, because of its presence in the library interface. Nevertheless, I don't argue for the extirpation of qualifiers, if only because it is too late."
`:cite-note-26`!21.`! `F0af`_`[↑`#cite-ref-26]`_`f Nim Manual: Const section
`:cite-note-27`!22.`! `F0af`_`[↑`#cite-ref-27]`_`f const (C# Reference)
`:cite-note-28`!23.`! `F0af`_`[↑`#cite-ref-28]`_`f `:citerefgoslingjoysteele`aGosling, James; Joy, Bill; Steele, Guy. "Java Language Specification Third Edition".
`:cite-note-29`!24.`! `F0af`_`[↑`#cite-ref-29]`_`f "Bug ID: JDK-4211070 Java should support const parameters (like C++) for code maintainence [`*sic`*]". Bugs.sun.com. Retrieved 2014-11-04.
`:cite-note-30`!25.`! `F0af`_`[↑`#cite-ref-30]`_`f 1815A, 3.2.1. Object Declarations Archived October 20, 2014, at the `F33f`_`[Wayback Machine`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Wayback_Machine]`_`f: "The declared object is a constant if the reserved word constant appears in the object declaration; the declaration must then include an explicit initialization. The value of a constant cannot be modified after initialization. Formal parameters of mode in of subprograms and entries, and generic formal parameters of mode in, are also constants; a loop parameter is a constant within the corresponding loop; a subcomponent or slice of a constant is a constant."
`:cite-note-32`!26.`! `F0af`_`[↑`#cite-ref-32]`_`f "const". `*MDN`*. Retrieved 2017-10-31.

>>External links

• "Const-Correctness" by `F33f`_`[Herb Sutter`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Herb_Sutter]`_`f
• "Constant Optimization?" by Herb Sutter
• The C++ FAQ Lite: Const correctness by Marshall Cline
• Section "Value substitution" from the free electronic book `*Thinking in C++`* by `F33f`_`[Bruce Eckel`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Bruce_Eckel]`_`f
• "Here A Const, There A Const" by `F33f`_`[Walter Bright`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Walter_Bright]`_`f
• "Const and Invariant" from D programming language specification, version 2 (experimental)

`c`F0af`_`[↑ Back to top`#top]`_`f`a